package com.lagou.base;

import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Map;

public class BaseServlet extends HttpServlet {

    /*
     * 将doGet方法最为一个调度器，根据请求功能的不同，调用对应的方法
     *       规定必须传递一个参数
     *               metodName= 功能名
     *
     * */

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) {

        //1.获取参数，要访问的方法名
        //String methodName = req.getParameter ("methodName");
        String methodName = null;

        //1.获取post请求的content-Type类型
        String contentType = req.getHeader ("Content-Type");

        //2.判断传递的数据是否为JSON格式
        if ("application/json;charset=uft-8".equals (contentType)) {
            //是JSON格式，调用getPostJSON
            String postJSON = getPostJSON (req);

            //将JSON格式的字符串转换为map
            Map<String,Object> map = JSON.parseObject (postJSON, Map.class);

            //从map集合中获取methodName
            methodName = (String) map.get ("methodName");

            //将获取到的数据，保存到request域对象中
            req.setAttribute ("map",map);
        } else {
            methodName = req.getParameter ("methodName");
        }
        //2.判断执行对饮的功能
        if(methodName != null) {
            try {
                //通过反射优化代码，提升代码打可维护性

                //1.获取字节码文件对象
                Class c = this.getClass ();

                //2.根据传入的方法名，获取对应的方法对象
                Method method = c.getMethod (methodName, HttpServletRequest.class, HttpServletResponse.class);

                //3.调用method对象的invoke，并执行对应的方法
                method.invoke (this, req, resp);
            } catch (Exception e) {
                e.printStackTrace ();
                System.out.println ("请求的功能不存在");
            }
        }
//        if("addCourse".equals (methodName)) {
//            addCourse (req,resp);
//        } else if ("findByName".equals (methodName)) {
//            findByNameCourse (req,resp);
//        } else if ("findByStatus".equals (methodName)) {
//            findByStatusCourse (req,resp);
//        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet (req, resp);
    }

    /*
    *
    * post请求为json：Content-Type : application/json ; charset=utf-8
    *
    * */
    public String getPostJSON(HttpServletRequest request) {

        try {
            //1.从reques中获取缓冲流输入对象
            BufferedReader reader = request.getReader ();
            //2.创建stringbuffer 保存读取出的数据
            StringBuilder sb = new StringBuilder ();

            //3.循环读取
            String line = null;
            while ((line = reader.readLine ()) != null) {
                //将每次读取的数据，追加到Stringbuffer
                sb.append (line);
            }

            //返回结果
            return sb.toString ();
        } catch (IOException e) {
            e.printStackTrace ();
            return null;
        }
    }
}
